package sim.states;

import sim.ISimulator;
import sim.ISimulator.State;
import utils.Point2D_DataPackage;
import ctf.network.ProtocolError;

/**
 * Class responsible for the parsing of all messages received in the play state.
 * 
 * @author N. Bhatia, A. Butt, N. Evans, C. Levesque, P. OKeefe
 * 
 */
public class PlayState extends AbstractState {
	public PlayState(ISimulator simulator) {
		super(simulator);
	}

	@Override
	public void request(int clientId, String request) throws ProtocolError {
		String[] splitString = request.split(" ");
		if (splitString[0].equals("getState") && splitString.length == 1) {
			String gameState = this.simulator.getGameState(clientId);
			if (gameState.startsWith("roundOver:")) {
				this.simulator.setState(clientId, State.NEW_ROUND);
			}
			this.simulator.respond(clientId, gameState);
			
		} else if (splitString[0].equals("spin") && splitString.length == 4) {
		
			this.simulator.spinPlayer(clientId, 
					Integer.parseInt(splitString[1]),
					Integer.parseInt(splitString[2]), 
					Float.parseFloat(splitString[3]));
			this.simulator.respond(clientId, "time: " + this.simulator.getTime());
			
		} else if (splitString[0].equals("accelerate")
				&& splitString.length == 4) {
			this.simulator.acceleratePlayer(clientId, 
					Integer.parseInt(splitString[1]),
					Integer.parseInt(splitString[2]), 
					Float.parseFloat(splitString[3]));
			this.simulator.respond(clientId, "time: " + this.simulator.getTime());


		} else if (splitString[0].equals("placePlayer") && splitString.length == 3) {
			Point2D_DataPackage point = new Point2D_DataPackage();
			point.decode(splitString[2]);
			this.simulator.placePlayer(clientId, 
					Integer.parseInt(splitString[1]), point.point);
			this.simulator.respond(clientId, "time: " + this.simulator.getTime());
		} else if (splitString[0].equals("setTime") && splitString.length == 2) {
			this.simulator.setTime(clientId, Integer.parseInt(splitString[1]));
			this.simulator.respond(clientId, "time: " + this.simulator.getTime());
		} else {
			throw new ProtocolError(
			// "getState | spin (Int) (Int) (Float) | accelerate (Int) (Int) (Float) | placePlayer (Int) (Float, Float) | setTime (Int)",
					"getState | Command", request);
		}

	}

}
